import raceApi from 'api/race'
// import constant from 'src/views/race/constant'
// state
let state = {
  list: [],
  previewForm: {},
  id: null,
  race_type: '',
  detail: {},
  race_rule: {}, // 获取赛事章程内容
  race_form: {},
  match_notice: {}, // 获取赛事公共内容
  edit_match_notice: {}, // 获取需要修改的赛事公告的内容
  match_detail: {},
  race_tem_manage: {}, // 球队数据
  race_team_manage_list: {}, // 球队成员
  race_player_list: {}, // 成员详细资料
  race_group_type: {}, // 个人跑步组别
  race_user_list: {}, // 个人参赛列表
  race_lottery_list: {}, // 摇号列表
  race_singer_info: {}, // 个人参赛者详细资料
  has_form: false, // 有待提交的表单
  race_weedour_teamlist: [], // 淘汰赛参赛球队
  race_weedout_vslist: [], // 淘汰赛对阵数据
  schedule: {}, // 赛程预览
  has_schedule: false,
  time_common: [], // 通用比赛时段
  race_cup_teams: [], // 杯赛球队列表
  race_cup_groups: [], // 杯赛分组列表
  race_cup_team_list: [], // 杯赛分组的球队
  race_dynamic_match_list: [], // 动态获取赛事报名字段
  race_add_member_btn: {},
  race_get_params: {}
}
// getters
const getters = {
  race_list: state => state.list,
  race_preview_form: state => state.previewForm,
  race_detail: state => state.detail,
  race_type: state => state.race_type,
  race_rule: state => state.race_rule,
  race_form: state => state.race_form,
  race_match_notice: state => state.match_notice,
  race_edit_match_notice: state => state.edit_match_notice,
  match_detail: state => state.match_detail,
  race_tem_manage: state => state.race_tem_manage,
  race_team_manage_list: state => state.race_team_manage_list,
  race_player_list: state => state.race_player_list,
  race_group_type: state => state.race_group_type,
  race_user_list: state => state.race_user_list,
  race_lottery_list: state => state.race_lottery_list,
  race_singer_info: state => state.race_singer_info,
  race_has_form: state => state.has_form,
  race_weedour_teamlist: state => state.race_weedour_teamlist,
  race_weedout_vslist: state => state.race_weedout_vslist,
  race_schedule: state => state.schedule,
  race_has_schedule: state => state.has_schedule,
  race_time_common: state => state.time_common,
  race_cup_teams: state => state.race_cup_teams,
  race_cup_groups: state => state.race_cup_groups,
  race_cup_team_list: state => state.race_cup_team_list,
  race_dynamic_match_list: state => state.race_dynamic_match_list,
  race_add_member_btn: state => state.race_add_member_btn,
  race_get_params: state => state.race_get_params
}

// actions
const actions = {
  race_list ({commit, state}, params) {
    raceApi.list(params).then((json) => {
      commit('RACE_LIST', json)
    })
  },
  race_preview ({commit, state}, params) {
    commit('RACE_PREVIEW', {
      obj: params
    })
  },
  race_detail ({commit, state}, params) {
    raceApi.detail(params).then((json) => {
      commit('RACE_DETAIL', json.data)
    })
  },
  race_rule ({commit, state}, params) {
    state.race_rule = {}
    raceApi.getRule(params).then((json) => {
      commit('RACE_RULE', json.data)
    })
  },
  race_match_notice ({commit, state}, params) {
    raceApi.matchNotice(params).then((json) => {
      commit('RACE_MATCH_NOTICE', json.data)
    })
  },
  race_edit_match_notice ({commit, state}, params) {
    raceApi.getEditMatchNotice(params).then((json) => {
      commit('RACE_EDIT_MATCH_NOTICE', json.data)
    })
  },
  race_tem_manage ({commit, state}, params) {
    raceApi.getParticipant(params).then((json) => {
      commit('RACE_TEM_MANAGE', json.data)
    }).catch((ERR) => {
      return false
    })
  },
  race_team_manage_list ({commit, state}, params) {
    // state.race_team_manage_list = {}
    raceApi.teamUserList(params).then((json) => {
      commit('RACE_TEAM_MANAGE_LIST', json.data)
    }).catch((ERR) => {
      return false
    })
  },
  race_player_list ({commit, state}, params) {
    state.race_player_list = {}
    raceApi.teamUserInfo(params).then((json) => {
      commit('RACE_PLAYER_LIST', json.data)
    }).catch((ERR) => {
      return false
    })
  },
  race_group_type ({commit, state}, params) {
    state.race_group_type = {}
    raceApi.groupType(params).then((json) => {
      commit('RACE_GROUP_TYPE', json.data)
    }).catch((ERR) => {
      return false
    })
  },
  race_user_list ({commit, state}, params) {
    // state.race_user_list = {}
    raceApi.getUsersList(params).then((json) => {
      commit('RACE_USER_LIST', json.data)
    }).catch((ERR) => {
      return false
    })
  },
  race_lottery_list ({commit, state}, params) {
    state.race_lottery_list = {}
    raceApi.getUsersList(params).then((json) => {
      commit('RACE_LOTTERY_LIST', json.data)
    }).catch((ERR) => {
      return false
    })
  },
  race_singer_info ({commit, state}, params) {
    raceApi.getSingerInfo(params).then((json) => {
      commit('RACE_SINGER_INFO', json.data)
    }).catch((ERR) => {
      return false
    })
  },
  race_weedour_teamlist ({commit, state}, params) {
    state.race_weedour_teamlist = []
    if (params.isCup) {
      raceApi.getCupKonckoutUser({matchId: params.matchId}).then((json) => {
        commit('RACE_WEEDOUR_TEAMLIST', json.data)
      }).catch((ERR) => {
        return false
      })
    } else {
      raceApi.getSeedUser({matchId: params.matchId}).then((json) => {
        commit('RACE_WEEDOUR_TEAMLIST', json.data)
      }).catch((ERR) => {
        return false
      })
    }
  },
  race_dynamic_match_list ({commit, state}, params) {
    raceApi.dynamicGetMatchList(params).then((json) => {
      commit('RACE_DYNAMIC_MATCH_LIST', json.data)
    }).catch((ERR) => {
      return false
    })
  },
  race_add_member_btn ({commit, state}, params) {
    raceApi.isAddMatchMember(params).then((json) => {
      commit('RACE_ADD_MEMBER_BTN', json)
    }).catch((json) => {
      commit('RACE_ADD_MEMBER_BTN', json)
      return false
    })
  },
  race_get_params ({commit}, params) {
    commit('RACE_GET_PARAMS', params)
  }
}
// mutations
const mutations = {
  RACE_LIST (state, data) {
    state.list = data.data ? data.data : []
  },
  RACE_PREVIEW (state, data) {
    state.previewForm = Object.assign({}, data)
  },
  RACE_FORM (state, data) {
    state.race_form = Object.assign({}, data)
  },
  RACE_DETAIL (state, data) {
    state.detail = data
  },
  UPDATE_NOTICE (state, obj) {
    state.noticeForm = Object.assign({}, obj)
  },
  RACE_SET_TYPE (state, data) {
    state.race_type = data
  },
  RACE_RULE (state, data) {
    state.race_rule = Object.assign({}, state.race_rule, data)
  },
  RACE_MATCH_NOTICE (state, data) {
    state.match_notice = Object.assign({}, state.match_notice, data)
  },
  RACE_EDIT_MATCH_NOTICE (state, data) {
    state.edit_match_notice = Object.assign({}, state.edit_match_notice, data)
  },
  MATCH_DETAIL (state, data) {
    state.match_detail = data
  },
  RACE_TEM_MANAGE (state, data) {
    state.race_tem_manage = data
  },
  RACE_TEAM_MANAGE_LIST (state, data) {
    state.race_team_manage_list = data
  },
  RACE_PLAYER_LIST (state, data) {
    state.race_player_list = data
  },
  RACE_GROUP_TYPE (state, data) {
    state.race_group_type = data
  },
  RACE_USER_LIST (state, data) {
    state.race_user_list = data
  },
  RACE_LOTTERY_LIST (state, data) {
    state.race_lottery_list = data
  },
  RACE_SINGER_INFO (state, data) {
    state.race_singer_info = data
  },
  RACE_HAS_FORM (state, payload) {
    state.has_form = payload
  },
  RACE_WEEDOUT_VSLIST (state, data) {
    state.race_weedout_vslist = data
  },
  RACE_WEEDOUR_TEAMLIST (state, data) {
    let teamData = []
    let total = 4
    if (data) { // 设置轮空数据
      if (data.length <= 4 && data.length > 0) {
        total = 4
      } else if (data.length <= 8 && data.length > 4) {
        total = 8
      } else if (data.length > 8 && data.length <= 16) {
        total = 16
      } else if (data.length <= 32 && data.length > 16) {
        total = 32
      } else if (data.length > 32 && data.length <= 64) {
        total = 64
      } else if (data.length > 64 && data.length <= 128) {
        total = 128
      }
      for (let i = 0; i < data.length; i++) {
        if (data[i]) {
          let userName = data[i].commingName || data[i].nickname
          let userName2 = data[i].doublePersonName
          teamData.push({
            value: data[i].userId,
            name: userName2 ? userName + '&' + userName2 : userName
          })
        }
      }
      for (let n = 0; n < total - data.length; n++) {
        teamData.push({
          value: 'null' + n,
          name: '轮空'
        })
      }
    }
    state.race_weedour_teamlist = teamData
  },
  RACE_SCHEDULE (state, obj) {
    state.schedule = obj.form
    state.time_common = obj.timeCommon
    state.has_schedule = true
  },
  RACE_HAS_SCHEDULE (state, bool) {
    state.has_schedule = bool
  },
  RACE_CUP_TEAMS (state, data) {
    let list = []
    for (let i = 0, l = data.length; i < l; i++) {
      list.push({
        label: (data[i].commingName || data[i].nickname) + (data[i].doublePersonName ? '&' + data[i].doublePersonName : ''),
        value: data[i].userId,
        disabled: false,
        iconUrl: data[i].iconUrl
      })
    }
    state.race_cup_teams = list
  },
  RACE_CUP_GROUPS (state, data) {
    state.race_cup_groups = data
  },
  RACE_CUP_TEAM_LIST (state, arr) {
    state.race_cup_team_list = arr
  },
  RACE_DYNAMIC_MATCH_LIST (state, data) {
    state.race_dynamic_match_list = data
  },
  RACE_ADD_MEMBER_BTN (state, data) {
    state.race_add_member_btn = data
  },
  RACE_GET_PARAMS (state, data) {
    state.race_get_params = data
  }
}

export default {
  state,
  getters,
  actions,
  mutations
}
